package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.reservation.vo.*;
import com.xiaoshuidi.cloud.module.rooms.convert.roomreservationconfig.RoomReservationConfigConvert;
import com.xiaoshuidi.cloud.module.rooms.enums.ReservationConfigStatusEnum;
import com.xiaoshuidi.cloud.module.rooms.enums.ReservationConfigTypeEnum;
import com.xiaoshuidi.cloud.module.rooms.enums.ReserveWithdrawReasonEnum;
import com.xiaoshuidi.cloud.module.rooms.pojo.Apartment;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomReservationConfig;
import com.xiaoshuidi.cloud.module.rooms.pojo.dto.reservation.RoomReservationConfigPageDTO;
import com.xiaoshuidi.cloud.module.rooms.repository.ApartmentRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomReservationRepository;
import com.xiaoshuidi.cloud.module.rooms.service.IRoomReservationConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公寓预定配置 服务实现类
 *
 * @author Feng
 * @since 2023-05-11
 */
@Service
@RequiredArgsConstructor
public class RoomReservationConfigServiceImpl implements IRoomReservationConfigService {

    private final RoomReservationRepository reservationRepository;
    private final ApartmentRepository apartmentRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(ReservationConfigUpdateReq req) {
        RoomReservationConfig config = reservationRepository.getById(req.getId());
        Assert.isTrue(ObjectUtil.isNotEmpty(config), "预定配置信息不存在");
        RoomReservationConfig roomReservationConfig = RoomReservationConfigConvert.INSTANCE.convert(req);
        roomReservationConfig.setStatus(ReservationConfigStatusEnum.SETUP.getCode());
        reservationRepository.updateById(roomReservationConfig);
        return true;
    }

    @Override
    public PageResult<ReservationConfigListResp> page(ReservationConfigPageReq req) {
        IPage<RoomReservationConfigPageDTO> page = reservationRepository.page(req.getPageNo(), req.getPageSize(), req.getApartmentId(), req.getApartmentName());
        List<RoomReservationConfigPageDTO> records = page.getRecords();
        List<ReservationConfigListResp> list = records.stream().map(s -> {
            ReservationConfigListResp resp = new ReservationConfigListResp();
            resp.setId(s.getId());
            resp.setApartmentName(s.getApartmentName());
            resp.setRetentionDays(s.getRetentionDays());
            resp.setStatus(s.getStatus());
            resp.setStrStatus(ReservationConfigStatusEnum.getValue(s.getStatus()));
            if (s.getDeposit() != null) {
                if (ReservationConfigTypeEnum.FIXED.getCode().equals(s.getDepositType())) {
                    resp.setDeposit(DecimalFormat.getInstance().format(s.getDeposit()));
                } else {
                    resp.setDeposit(String.format("%s%%", DecimalFormat.getInstance().format(s.getDeposit())));
                }
            }
            return resp;
        }).collect(Collectors.toList());
        PageResult<ReservationConfigListResp> pageResult = new PageResult<>(list, page.getTotal());
        return pageResult;
    }

    @Override
    public ReservationConfigResp get(Integer id) {
        RoomReservationConfig config = reservationRepository.getById(id);
        Assert.isTrue(ObjectUtil.isNotEmpty(config), "预定配置不存在");
        Apartment apartment = apartmentRepository.getById(config.getApartmentId());
        ReservationConfigResp resp = RoomReservationConfigConvert.INSTANCE.convert(config);
        resp.setApartmentName(apartment.getName());
        return resp;
    }

    @Override
    public Long create(ReservationConfigCreateReq req) {
        RoomReservationConfig config = reservationRepository.getByApartmentId(req.getApartmentId());
        Assert.isTrue(ObjectUtil.isEmpty(config),"该公寓预定配置已存在");
        RoomReservationConfig roomReservationConfig = RoomReservationConfigConvert.INSTANCE.convert(req);
        reservationRepository.save(roomReservationConfig);
        return roomReservationConfig.getId();
    }

    @Override
    public ReservationConfigGetResp getConfig(Long apartmentId) {
        RoomReservationConfig config = reservationRepository.getByApartmentId(apartmentId);
        Assert.isTrue(ObjectUtil.isNotEmpty(config),"预定配置不存在");
        ReservationConfigGetResp resp = RoomReservationConfigConvert.INSTANCE.transform(config);
        // 退定原因
        List<JSONObject> withdrawReason = new ArrayList<>();
        if (resp.getNoRefund()){
            JSONObject reason = new JSONObject();
            reason.put("id",ReserveWithdrawReasonEnum.DEFAULT.getValue());
            reason.put("name",ReserveWithdrawReasonEnum.DEFAULT.getName());
            withdrawReason.add(reason);
        }
        if (resp.getConsultRefund()){
            JSONObject reason = new JSONObject();
            reason.put("id",ReserveWithdrawReasonEnum.NEGOTIATE.getValue());
            reason.put("name",ReserveWithdrawReasonEnum.NEGOTIATE.getName());
            withdrawReason.add(reason);
        }
        resp.setWithdrawReasonList(withdrawReason);
        return resp;
    }
}
